package com.starwish.starwishblog.web.back;

import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Manager;
import com.starwish.starwishblog.result.ResultResponse;
import com.starwish.starwishblog.service.ManagerService;
import com.starwish.starwishblog.vo.AccountVo;
import com.starwish.starwishblog.vo.ManagerVo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("admin/manager")
@Api(value = "管理员的操作")
public class MangerController {

    @Autowired
    ManagerService managerService;
    @Autowired
    StringRedisTemplate stringRedisTemplate ;

    @PostMapping("/login")
    @ApiOperation(value = "管理员登录的操作")
    public ResultResponse<Manager> login(@ApiParam(name="AccountVo",value = "接收用户名和密码") @RequestBody AccountVo accountVo){
        ResultResponse<Manager> result = null;
        String code1 = stringRedisTemplate.opsForValue().get("code");
        String code = accountVo.getCode();
        if(code1.equals(code)||"0000".equals(code1)){
            try {
                Manager manager = managerService.login(accountVo.getAdminName());
                if(manager == null){
                    //账户不存在
                    result = new ResultResponse<>(201,"账户不存在");
                }else if(manager.getPassword().equals(accountVo.getPassword())){
                    //登录成功
                    result = new ResultResponse<>(200,"登录成功",manager);
                    //进行认证处理
                    //生成一个字符串  返回到前端
//                JwtInfo jwtInfo = new JwtInfo();
//                jwtInfo.setUsername(manager.getUserName());
//                String token = JwtUtil.createToken(jwtInfo);
//                result.setToken(token);
                }else{
                    //密码错误
                    result = new ResultResponse<>(202,"密码错误");
                }
            } catch (Exception e) {
                //系统错误
                result = new ResultResponse<>(305,"系统错误");
                e.printStackTrace();
            }
        }else{
            result = new ResultResponse<>(203,"验证码错误");
        }
        return result;
    }

    @GetMapping("/list")
    public ResultResponse<PageInfo<ManagerVo>> list(@RequestParam(name="now",defaultValue = "1")Integer now,
                                                    @RequestParam(name="size",defaultValue = "5")Integer size,
                                                    String name){
        ResultResponse<PageInfo<ManagerVo>> result = null;
        try {
            PageInfo<ManagerVo> pageInfo = managerService.queryManagerVoByCons(now,size,name);
            result = new ResultResponse<>(200,"查询管理员成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询管理员失败");
        }
        return result;
    }


    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody ManagerVo managerVo){
        ResultResponse<Void> result = null;
        try {
            Manager oldManager = managerService.queryManagerByKey(managerVo.getId());
            managerVo.setCreateTime(oldManager.getCreateTime());
            int i = managerService.modify(managerVo);
            if(i > 0){
                result = new ResultResponse<>(200,"修改成功");
            }else{
                result = new ResultResponse<>(201,"修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

    @PutMapping("/add")
    public ResultResponse<Void> add(@RequestBody ManagerVo managerVo){
        ResultResponse<Void> result = null;
        try {
            int i = managerService.addManager(managerVo);
            if(i > 0){
                result = new ResultResponse<>(200,"添加成功");
            }else{
                result = new ResultResponse<>(201,"修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

    @DeleteMapping("/del/{id}")
    public ResultResponse<Void> delById(@PathVariable(name="id")Integer id){
        ResultResponse<Void> result = null;
        try {
            int i = managerService.removeByKey(id);
            if(i > 0){
                result = new ResultResponse<>(200,"删除成功");
            }else{
                result = new ResultResponse<>(201,"删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }

    @GetMapping("/changeLocked")
    public ResultResponse<Void> changeLocked(@RequestParam("id")Long id,@RequestParam("status") Integer status){
        ResultResponse<Void> result = null;
        try {
            int i = managerService.updateLocked(id,status);
            if(i > 0){
                result = new ResultResponse<>(200,"修改成功");
            }else{
                result = new ResultResponse<>(201,"修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }
    @GetMapping("/loginOut")
    @ApiOperation("用户退出方法")
    public ResultResponse<Void>  loginOut(){
        ResultResponse<Void> result = null;
        try {
            result = new ResultResponse<>(200,"退出登录");
        }catch (Exception e){
            e.printStackTrace();
            result = new ResultResponse<>(205,"系统错误");
        }
        return result;
    }
}
